Skip to content

fix: reject path template holes that share a segment with literal text#132

Merged
OmarAlJarrah merged 3 commits into
mainfrom
b1-pathtemplate-resegment
Jul 19, 2026
Merged

fix: reject path template holes that share a segment with literal text#132
OmarAlJarrah merged 3 commits into
mainfrom
b1-pathtemplate-resegment

Conversation

@OmarAlJarrah

Copy link
Copy Markdown
Member

Summary

  • PathTemplate.parse never required a {hole} to sit at a / boundary, so a literal prefix or suffix sharing a path segment with a hole (e.g. /reports/{id}.json or v{version}) got silently re-segmented when the URL was built: BindingExecutor's appendTokenSegments splits every Literal token on / and adds every Hole token as its own segment, regardless of whether it was adjacent to literal text in the template.
  • @PathTemplate frames itself as a "Go-style path template", and net/http.ServeMux wildcards must occupy a full path segment, so the fix rejects a boundary violation at parse time (fail fast) rather than trying to keep same-segment literal/hole runs together during composition. A hole is still allowed to sit directly against another hole with no literal between them ({a}{b}), since neither side can be split.
  • Documented the new constraint on the @PathTemplate KDoc.

Test plan

  • ./gradlew :kuri-bind:jvmTest — added regression tests reproducing both issue examples (/reports/{id}.json with id=5, v{version} with version=2) at both the PathTemplate.parse level and end-to-end through BindingExecutor; confirmed they failed before the fix and pass after.
  • ./gradlew :kuri-bind:ktlintCheck
  • ./gradlew :kuri-bind:detekt
  • ./gradlew :kuri-bind:apiCheck (no public API shape change — kuri-bind is JVM-only, so no jsNodeTest/native leg applies here)

Closes #82

PathTemplate.parse never required a {hole} to sit at a "/" boundary, so
a literal prefix or suffix sharing a path segment with a hole (e.g.
"/reports/{id}.json" or "v{version}") got silently re-segmented when
the URL was built: BindingExecutor's appendTokenSegments splits every
Literal token on "/" and adds every Hole token as its own segment,
independent of whether they were adjacent in the template.

@PathTemplate documents itself as a "Go-style path template", and
net/http.ServeMux wildcards must occupy a full path segment, so fail
fast at parse time instead: a hole whose neighboring literal doesn't
carry a "/" at the shared edge is now rejected with KuriBindException.
Adjacent holes with no literal between them (e.g. "{a}{b}") are still
allowed, since neither can be split.

Closes #82
The "hole must occupy a whole path segment" rule previously exempted a
hole from the boundary check whenever it abutted another hole, on the
theory that neither side could be split. But the composer always emits
one full segment per hole regardless of adjacency, so a template like
"{a}{b}" silently composed as two separate path segments even though
its spelling implies a single merged one. Treat two adjacent holes the
same as a hole abutting literal text: they now require a "/" between
them, and only a true template boundary (start/end of string) is
exempt.
Every template string in the existing suite starts with a literal (usually
a leading "/"), so isBoundedBefore's null-predecessor branch — a hole
sitting as the very first token, e.g. "{id}/detail" — was never exercised.
The symmetric end-of-template case was already covered via the catch-all
test. Add a test that parses "{id}/detail" and asserts it succeeds with
tokens [Hole("id"), Literal("/detail")], isolating the previously-untested
branch.
@OmarAlJarrah
OmarAlJarrah merged commit 111072b into main Jul 19, 2026
13 checks passed
@OmarAlJarrah
OmarAlJarrah deleted the b1-pathtemplate-resegment branch July 19, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kuri-bind: @PathTemplate holes sharing a path segment with literal text are silently re-segmented

1 participant